package Fonction;

import ihm.FenetreAjoutConsultation;
import ihm.FenetreAjoutSoinConsult;
import ihm.FenetreModifConsult;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;


public class ButtonEditorJTable extends DefaultCellEditor
{
	
    protected JButton button;
    private String label;
    private boolean isPushed;
    private String statut;
    private ButtonListener bListener = new ButtonListener ();
    
    public ButtonEditorJTable(String statut, JCheckBox checkBox) 
    {
        super(checkBox);
        button = new JButton();
        button.setOpaque(true);
        button.addActionListener(bListener);
        this.statut = statut;
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
            boolean isSelected, int row, int column) 
    {
    	bListener.setStatut(statut);
    	bListener.setRow(row);
    	bListener.setTable(table);
    	bListener.setColumn(column);
    	if (isSelected) {
            button.setForeground(table.getSelectionForeground());
            button.setBackground(table.getSelectionBackground());
        } else {
            button.setForeground(table.getForeground());
            button.setBackground(table.getBackground());
        }
        label = (value == null) ? "" : value.toString();
        button.setText(label);
        isPushed = true;
        return button;
    }

    @Override
    public Object getCellEditorValue() {
        isPushed = false;
        return label;
    }

    @Override
    public boolean stopCellEditing() {
        isPushed = false;
        return super.stopCellEditing();
    }

    @Override
    protected void fireEditingStopped() {
        super.fireEditingStopped();
    }
    
    public class ButtonListener implements ActionListener  
    {
    	 private int column;
    	 private int  row;
    	 private String statut;
         private JTable table;
          
         public void setColumn(int col){this.column = col;}
         public void setRow(int row){this.row = row;}
         public void setTable(JTable table){this.table = table;}
         public void setStatut(String statut){this.statut = statut;}
         @Override
         public void actionPerformed(ActionEvent evt) 
         {
        	 fireEditingStopped();
        	 String id = (String) this.table.getValueAt(this.row, 0);
        	 if(id.equals("Nouveau"))
        	 {
        		FenetreAjoutSoinConsult ajSoin = new FenetreAjoutSoinConsult(statut);
     			ajSoin.setModal(true);
     			ajSoin.setVisible(true);
        	 }
        	 else
        	 {
        		int rep = JOptionPane.showConfirmDialog(null, "Etes vous sur de vouloir supprimer ce soin ?","Message",JOptionPane.YES_NO_OPTION);
 				if (rep == JOptionPane.YES_OPTION)
 				{
 					if(statut.equals("FenetreModifConsult"))
 						FenetreModifConsult.supprimerSoin(row);
 					else
 						FenetreAjoutConsultation.supprimerSoin(row);
 				}
        	 }
         }      
     }
}
